import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:rembercount/controller/themeController.dart';
import 'package:rembercount/controller/countController.dart';

/// 分类明细页面，展示某个分类下的所有记账明细
/// 通过categoryName参数动态筛选数据
class CategoryDetailPage extends StatelessWidget {
  /// 分类名称
  final String categoryName;
  /// 所有记账明细（可选，若未传则自动从CountController获取并筛选）
  final List<Map<String, dynamic>>? allRecords;

  const CategoryDetailPage({super.key, required this.categoryName, this.allRecords});

  @override
  Widget build(BuildContext context) {
    final themeController = Get.find<ThemeController>();
    final countController = Get.find<CountController>();

    // 获取所有明细数据，优先用传入的allRecords，否则从controller获取
    final List<Map<String, dynamic>> records = allRecords ?? List<Map<String, dynamic>>.from(countController.categoryList.value ?? []);

    // 过滤出当前分类的明细
    final List<Map<String, dynamic>> details = records.where((item) => item['category'] == categoryName).toList();

    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        backgroundColor: Colors.white,
        elevation: 0,
        title: Text(
          '$categoryName明细',
          style: TextStyle(
            color: themeController.themeColor,
            fontWeight: FontWeight.bold,
            fontSize: 20,
          ),
        ),
        iconTheme: IconThemeData(color: themeController.themeColor),
      ),
      body: details.isEmpty
          ? Center(
              child: Text(
                '暂无明细',
                style: TextStyle(color: Colors.grey[400], fontSize: 16),
              ),
            )
          : ListView.separated(
              padding: const EdgeInsets.all(16),
              itemCount: details.length,
              separatorBuilder: (_, __) => const SizedBox(height: 12),
              itemBuilder: (context, index) {
                final item = details[index];
                // 单条明细卡片
                return Container(
                  decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(16),
                    boxShadow: [
                      BoxShadow(
                        color: Colors.grey.withOpacity(0.08),
                        blurRadius: 8,
                        spreadRadius: 1,
                        offset: const Offset(0, 2),
                      ),
                    ],
                  ),
                  child: ListTile(
                    contentPadding: const EdgeInsets.symmetric(horizontal: 20, vertical: 8),
                    leading: CircleAvatar(
                      backgroundColor: themeController.themeColor.withOpacity(0.15),
                      child: Icon(Icons.receipt_long, color: themeController.themeColor),
                    ),
                    title: Text(
                      item['note'] ?? '',
                      style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
                    ),
                    subtitle: Text(
                      item['date'] ?? '',
                      style: const TextStyle(color: Colors.grey, fontSize: 13),
                    ),
                    trailing: Text(
                      '¥${item['amount']}',
                      style: TextStyle(
                        color: themeController.themeColor,
                        fontWeight: FontWeight.bold,
                        fontSize: 16,
                      ),
                    ),
                  ),
                );
              },
            ),
    );
  }
} 